class StringUtil {
  static convertListToString(List<String> list, {String concatSymbol}) {
    if (concatSymbol == null) {
      concatSymbol = ",";
    }
    String result = '';
    if (list == null || list.isEmpty) {
      return result;
    }
    list.forEach((item) {
      result += (item + concatSymbol);
    });
    result = result.substring(0, result.length - concatSymbol.length);
    return result;
  }

  static convertListToString2(List list, {String concatSymbol}) {
    if (concatSymbol == null) {
      concatSymbol = ",";
    }
    String result = '';
    if (list == null || list.isEmpty) {
      return result;
    }
    list.forEach((item) {
      result += (item + concatSymbol);
    });
    result = result.substring(0, result.length - concatSymbol.length);
    return result;
  }

  static String getListItemByIndex(List<String> list, String index) {
    String result;
    try {
      int indexInt = int.parse(index) - 1;
      if (indexInt > -1 && list != null && indexInt < list.length) {
        result = list[indexInt];
      }
    } catch (e) {
      print(e);
    }
    return result;
  }

  static bool isEmpty(String s) {
    return s == null || s == '' || s.trim() == '';
  }

  static bool isNotEmpty(String s) {
    return !isEmpty(s);
  }

  static String getNotNullString(String s) {
    if (isEmpty(s)) {
      return '';
    }
    return s;
  }

  static bool isTrue(String s) {
    return s != null && s == '1';
  }

  static String getBoolValue(bool flag) {
    return flag ? '是' : '否';
  }

  static String getEliminateString(String str, int length) {
    if (isEmpty(str) || str.length < length) {
      return str;
    }
    return '${str.substring(0, length)}...';
  }

  static bool isValidIp(String input){
    RegExp ip = new RegExp(r"((http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)$");
    return ip.hasMatch(input);
  }
}
